<?php
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')
&& !$perm->userHasRole($AppUI->user_id, 'Marketing')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Management')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Staff')
&& !$perm->userHasRole($AppUI->user_id, 'General_Management')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('REFERENCE_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('REFERENCE_POST');
	}
	else {
		$_POST = $AppUI->getPOST('REFERENCE_POST');
	}
}
$AppUI->savePlace();

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 10));
// Check search or search all
if(isset($_POST['resetAction'])){
	// Remove param search
	unset($_POST['resetAction']);
	unset($_POST['keyword']);
	unset($_POST['project_company']);
	unset($_POST['project_rstatus']);
}
$customer = intval( dPgetParam( $_POST, 'project_company', 0 ) );
$status_id = intval( dPgetParam( $_POST, 'project_rstatus', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );

$canViewSalesAmount = $perm->checkModuleItem("taskfee", "view");
$canEditSalesAmount = $perm->checkModuleItem("taskfee", "edit");
$canViewExpense = $perm->checkModuleItem('task_expense', 'view');
$canDelete = $perm->checkModuleItem("projects", "delete");
// Load Object Status
$objMD = new CMdStatus();
$statList = NULL;
$hasRoleAccounting = $perm->userHasRole($AppUI->user_id, "Accounting");
if ($hasRoleAccounting) {
	$complete_status = new CMDStatus();
	$project_completed = dPgetSysVal( 'ProjectCompleted' );
	$complete_status->loadByCode($project_completed[0]);
	$statList = array($complete_status->status_id => $complete_status->status_name);
} else {
	if (count($_POST) == 0 && $status_id <= 0 && ($perm->userHasRole($AppUI->user_id, 'General_Management') || $perm->userHasRole($AppUI->user_id, 'FFIA_Management') || $perm->userHasRole($AppUI->user_id, 'FFIA_Staff'))) {
		$open_status = new CMDStatus();
		$project_open = dPgetSysVal( 'ProjectOpen' );
		$open_status->loadByCode($project_open[0]);
		$status_id = $open_status->status_id;
		$_POST['project_rstatus'] = $status_id;
	}
	$statList = $objMD->getStatusList('Reference');
}

$pro = new CProject();
$totalRows = 0;
$projects = $pro->getList($_POST, $totalRows, $page_limit, $page_offset);

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>"" . $AppUI->_(''). ""), $cusList);

?>
<form name="reportFrm" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
<input type="hidden" name="offset">
<table cellspacing="0" cellpadding="3" border="0" width="100%" class="infopanel">
<tr>
	<td width="70px" align="right" nowrap><?=$AppUI->_('Keyword')?></td>
	<td>
		<input type="text" class="text" name="keyword" id="keyword" value="<?=$keyword?>" style="width: 250px" />
	</td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Customer')?></td>
	<td ><?= arraySelect($cusList, 'project_company', ' id="customer" size="1" class="text" style="width: 257px"', $customer, true)?></td>
</tr>
<tr>
	<td align="right" nowrap><?=$AppUI->_('Status')?></td>
	<td><?= arraySelect($statList, 'project_rstatus', ' id="statusId" size="1" class="text" style="width: 100px;"', $status_id, true)?></td>
</tr>
</table>
<table cellspacing="0" cellpadding="6" border="0" width="100%" class="subinfopanel">
<tr>
	<td>
		<input class="button" type="button" name="searchAction" value="<?=$AppUI->_('Search');?>" onClick="doSearch(this.form)"/>
		<input class="button" type="submit" name="resetAction" value="<?=$AppUI->_('Reset');?>"/>
	</td>
</tr>
</table>
</form>
<table id="idReferenceList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
<?php
	$rowspan = $canViewExpense ? 2 : 1;
?>
	<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('%')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Code')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Name')?></th>
		<?php if($canViewExpense):?>
		<th colspan="3" align="center"><?=$AppUI->_('Expense amount')?></th>
		<?php endif;?>
		<?php if ($canViewSalesAmount):?>
			<th rowspan='<?=$rowspan?>'><?=$AppUI->_('Sales amount')?></td>
			<th rowspan="<?=$rowspan?>"><?=$AppUI->_('VAT amount')?></th>
			<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Account Receivable')?></th>
		<?php endif;?>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Status')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Type')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Shipper')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Shipping type')?></th>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Remarks')?></th>
		<?php if ($canDelete): ?>
		<th rowspan="<?=$rowspan?>"><?=$AppUI->_('Delete')?></th>
		<?php endif;?>
	</tr>
	<?php if($canViewExpense):?>
	<tr>
      <th ><?=$AppUI->_('Inter')?></th>
      <th ><?=$AppUI->_('On behalf')?></th>
      <th ><?=$AppUI->_('Other')?></th>
    </tr>
	<?php endif;?>
<?php
if (is_array($projects) && count($projects) > 0) {
	require_once($AppUI->getLibraryClass('codeigniter/pagination'));
	$pagination = new CI_Pagination();
	$config = array(
		'base_url' => "?m=$m&tab=$tab",
		'total_rows' => $totalRows,
		'per_page' => $page_limit

	);
	$pagination->initialize($config);
	?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.reportFrm.offset.value = offset;
			document.reportFrm.submit();
		}
	</script>
	<?
	$i = 1;
	$lastCompany = 0;
	foreach ($projects as $row) {
		//showFVar($row);
		$i++;
		if ($lastCompany != $row['project_company']) {
			$lastCompany = $row['project_company'];
			$companyURL = "?m=companies&a=view&company_id=" . $lastCompany;
	?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<td colspan="100%">
				<a href="<?=$companyURL?>" style="border: 1px outset rgb(238, 238, 238); background-color: rgb(255, 255, 255); padding: 3px;"><b><?=$row['company_name']?></b></a>
			</td>
		</tr>
	<?php
		$i++;
		}
		?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>" >
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?php printf( '%.1f%%', $pro->getCompletedPercent($row['project_id']));?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_iso_code']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_name']?></td>
			<?php if($canViewExpense):?>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getExpenseAmount($row['project_id'], 1))?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getExpenseAmount($row['project_id'], 2))?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getExpenseAmount($row['project_id'], 3))?></td>
			<?php endif;?>
			<?php if ($canViewSalesAmount):?>
			<td align='right'>
			<?php if (isset($row['project_actual_budget']) && $row['project_actual_budget'] > 0):?>
				<?=number_format($row['project_actual_budget'], 2)?>
			<?php else:?>
				<?=currencyFormat($row['sa_sales_amount'], $row['project_currency'])?>
			<?php endif;?>
			</td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getVATAmount($row['project_id']))?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getAccountReceivable($row['project_id']))?></td>
			<?php endif;?>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['status_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_type_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['sup_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['shipping_type_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_description']?></td>
			<?php if ($canDelete) {
			echo "<td align='center'>";
				showIconLink('images/delete.gif', 'js:doDelete('.$row["project_id"]. ', \''.addslashes($row["project_name"]).'\')', 'Delete');
			echo "</td>";
			}
			?>
		</tr>
		<?
	}
	?>
	<tr>
		<th colspan="100%"><?=$pagination->create_links();?></th>
	</tr>
	</table>
	<?
} else {
	showEmptyRow('1', 'second', 'Not found any references', '100%');
}

?>
</table>
<script>
	Calendar.enabled = true;

	Table("idReferenceList").setNormalHover(1);

	function showReference(id){
		location.href = "index.php?m=projects&a=view&project_id=" + id;
	}

	function doSearch(frm) {
		frm.action = '?m=<?=$m?>&a=<?=$a?>&suppressHeaders=0';
		frm.submit();
	}

	function doExport(frm) {
		frm.action = '?m=reports&a=do_r_reference&suppressHeaders=1';
		frm.submit();
	}
</script>
<form name="MdFormDelete" id="MdFormDelete" action="?m=projects" method="post">
	<input type="hidden" name="dosql" value ="do_delete" />
	<input type="hidden" id="del_cc_id" name="project_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>